package com.flexrrb.controls
{
	import com.flexrrb.resizeManagerClasses.Border;

	import flash.events.MouseEvent;
	import flash.geom.Point;

	import mx.core.EdgeMetrics;
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	import mx.managers.CursorManagerPriority;

	public class ResizeManager extends UIComponent
	{
		public function ResizeManager()
		{
			super();
		}

		private var cursorID:int=CursorManager.NO_CURSOR;

		[Bindable]
		[Embed(source="/assets/resizeCursorH.gif")]
		public static var widthCur:Class;

		[Bindable]
		[Embed(source="/assets/resizeCursorV.gif")]
		public static var heightCur:Class;

		protected var _target:UIComponent;
		protected var _leftBorder:Border;
		protected var _rightBorder:Border;
		protected var _bottomBorder:Border;
		protected var _topBorder:Border;
		private var currentBorder:String;

		private var _anchorsRequired:Boolean=true;

		[Inspectable(enumeration="false,true", defaultValue="true")]

		public function get anchorsRequired():Boolean
		{
			return _anchorsRequired;
		}

		public function set anchorsRequired(value:Boolean):void
		{
			_anchorsRequired=value;
		}

		public function get target():UIComponent
		{
			return _target;
		}

		public function set target(value:UIComponent):void
		{
			_target=value;
			createBorders();
		}

		private function createBorders():void
		{
			_leftBorder=new Border("left", target.width, target.height);
			//_leftBorder.alpha=0;
			addChild(_leftBorder);
			_rightBorder=new Border("right", target.width, target.height);
			//_rightBorder.alpha=0;
			addChild(_rightBorder);
			_bottomBorder=new Border("bottom", target.width, target.height);
			//_bottomBorder.alpha=0;
			addChild(_bottomBorder);
			_topBorder=new Border("top", target.width, target.height);
			//_topBorder.alpha=0;
			addChild(_topBorder);
			addEventListener(MouseEvent.MOUSE_DOWN, borderMouseDownHanlder);
			addEventListener(MouseEvent.MOUSE_OVER, borderMouseOverHanlder);
			addEventListener(MouseEvent.MOUSE_OUT, borderMouseOutHanlder);
		}

		private function borderMouseOverHanlder(event:MouseEvent):void
		{
			changeCursor((event.target as Border).borderType);
		}

		private function borderMouseOutHanlder(event:MouseEvent):void
		{
			restoreCursor();
		}

		private function borderMouseDownHanlder(event:MouseEvent):void
		{
			currentBorder=(event.target as Border).borderType;
			changeCursor(currentBorder);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, scaleComp);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopScale);
		}

		private function changeCursor(type:String):void
		{
			if (cursorID == CursorManager.NO_CURSOR && type)
			{
				var cursorClass:Class=(type == "left" || type == "right") ? widthCur : heightCur;
				var xOffset:Number=(type == "left" || type == "right") ? -11 : -5.5;
				var yOffset:Number=(type == "left" || type == "right") ? -5.5 : -11;
				cursorID=cursorManager.setCursor(cursorClass, CursorManagerPriority.HIGH, xOffset, yOffset);
			}
			else
			{
				restoreCursor();
			}
		}

		/**
		 *  @private
		 */
		private function restoreCursor():void
		{
			if (cursorID != CursorManager.NO_CURSOR)
			{
				cursorManager.removeCursor(cursorID);
				cursorID=CursorManager.NO_CURSOR;
			}
		}

		protected function scaleComp(e:MouseEvent):void
		{
			var point:Point=new Point(target.x, target.y);
			point=target.localToGlobal(point);
			if (currentBorder == "left")
			{
				if ((stage.mouseX - point.x) > target.minWidth)
					target.width=(stage.mouseX - point.x);

			}
			else if (currentBorder == "right")
			{
				if ((stage.mouseX - point.x) > target.minWidth)
					target.width=(stage.mouseX - point.x);

			}
			else if (currentBorder == "bottom")
			{
				if ((stage.mouseY - point.y) > target.minHeight)
					target.height=(stage.mouseY - point.y);
			}
			else if (currentBorder == "top")
			{
				if ((stage.mouseY - point.y) > target.minHeight)
					target.height=(stage.mouseY - point.y);
			}
			else
			{

			}
			invalidateDisplayList();
		}

		protected function stopScale(e:MouseEvent):void
		{
			restoreCursor();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, scaleComp);
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopScale);
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if (target)
			{
				var bm:EdgeMetrics;

				bm=EdgeMetrics.EMPTY;
				_leftBorder.scaleY=target.height / 3;
				_rightBorder.scaleY=target.height / 3;
				_bottomBorder.scaleX=target.width / 3;
				_topBorder.scaleX=target.width / 3;
				_leftBorder.x=target.x;
				_rightBorder.x=target.width;
				_bottomBorder.y=target.height;
				_topBorder.y=target.y;
			}
		}

		private function generateAnchors():void
		{
			//var 
		}
	}
}